import { _decorator, Component, Node, Vec3 } from "cc";
import { EventType } from "db://assets/Scripts/Enums/Events/EventsEnum";
import { SaveBtnManager } from "db://assets/Scripts/Manager/SaveBtnManager";
import { SceneManager } from "db://assets/Scripts/Manager/SceneManager";
import { CustomEventListener } from "db://assets/Scripts/Listener/CustomEventListener";
const { ccclass, property } = _decorator;

@ccclass("SceneMgrAssist")
export class SceneMgrAssist extends Component {
  @property
  parentClass: SceneManager;

  bindListenerMethod(): void {
    const listener = CustomEventListener.getInstance();
    listener.on(
      EventType.START_NEW_GAME_EVENT,
      this.controllerWithButton,
      this
    );
    listener.on(
      EventType.CANCEL_START_NEW_GAME,
      this.controllerWithButton,
      this
    );
    listener.on(EventType.SUBMIT_NICK_NAME, this.controllerWithButton, this);
  }

  
  controllerWithButton(data: string) {
    const v3 = new Vec3(0, 0, 0);

    if (data === EventType.START_NEW_GAME_EVENT) {
      this.parentClass.commonUtil.switchSetActive(
        this.parentClass.nodesArray,
        this.parentClass.regPageNode
      );
      this.parentClass.regPageNode.setPosition(v3);
    } else if (data === EventType.CANCEL_START_NEW_GAME) {
      this.parentClass.commonUtil.switchSetActive(
        this.parentClass.nodesArray,
        this.parentClass.beginInterNode
      );
    } else if (data === EventType.SUBMIT_NICK_NAME) {
      const saveBtnComp = this.parentClass.addComponent(SaveBtnManager);
      const flag = saveBtnComp.dealWithInput();
      if (flag) {
        // 显示战场
        this.parentClass.commonUtil.switchSetActive(
          this.parentClass.nodesArray,
          this.parentClass.battleFieldNode
        );
        this.parentClass.battleFieldNode.setPosition(v3);
      }
    }
  }

  start() {
    this.name = "SceneMgrAssist";
  }

  onLoad() {
    this.bindListenerMethod();
  }
}
